{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import win32com.client\n",
    "from win32com.client import Dispatch\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "global unisim\n",
    "global simcase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def open_simulation(path):\n",
    "    unisim = win32com.client.Dispatch('UniSimDesign.Application')\n",
    "    unisim.Visible = True\n",
    "    simcase = unisim.SimulationCases.Open(path)\n",
    "    return simcase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_temperature_value(simcase,stream_name):\n",
    "    stream = simcase.Flowsheet.MaterialStreams.Item(stream_name)\n",
    "    temperature_value = stream.TemperatureValue #C\n",
    "    return temperature_value\n",
    "def set_flowrate_value(simcase,stream_name,parameter,value):\n",
    "    stream = simcase.Flowsheet.MaterialStreams.Item(stream_name)\n",
    "    flowrate_value = stream.IdealLiquidVolumeFlowValue=value/3600\n",
    "def reset_sim_time(simcase):\n",
    "    simcase.Solver.Integrator.Reset()\n",
    "def run_dynamic_sim(simcase,simulation_time):\n",
    "    simcase.Solver.Integrator.RunFor(simulation_time)\n",
    "def get_current_time(simcase):\n",
    "    #Get the current time of the dynamic simulation\n",
    "    return simcase.Integrator.CurrentTimeValue/60\n",
    "def change_valve_opening(simcase,valve_name,opening_value):#Manipulate a valve opening in the dynamic simulation\n",
    "    simcase.Flowsheet.Operations(valve_name).ActuatorSPValue=opening_value\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_path = os.getcwd()\n",
    "simcase = open_simulation(os.path.join(base_path,'ammonia_15.5Mpa_100%.usc'))\n",
    "f = simcase.Flowsheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1 = f.Streams('1')\n",
    "f1.MassFlow.GetValue('kg/h'),f1.MolarFlow.GetValue('kgmole/h'),f1.MolarFlow.SetValue(8000,'kgmole/h')\n",
    "J1 = f.Operations('103-J1')\n",
    "J1.DeltaP.GetValue('kpa')\n",
    "J1.DeltaP.SetValue(6000,'kpa') #无法设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "INPUTS = f.Operations('INPUTS')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "A1 = INPUTS.Cell('A1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A1.Units  # 获得单位\n",
    "A1.VariableName # 获得名称/变量描述 \n",
    "A1.AttachedObjectName # 获得流股/设备编号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SYS_AD = f.Operations('SYS-AD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simcase.Flowsheet.Operations('SYS-AD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关闭模拟文件\n",
    "simcase.Close()\n",
    "simcase.Activate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "INPUTS.NumberOfRows"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('dejavu')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9e192e4b83075e3ddca6984e464aed66f2cf89325ff1b87b1a375e27388972c7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
